﻿namespace NJQuery {
	using System;
	using System.Web.UI;
	using System.Web.UI.WebControls;
	using System.ComponentModel;
	using System.Diagnostics.CodeAnalysis;

	/// <summary>
	/// Server control used within the <see cref="Accordion"/> control to generate inner markup needed for each panel of a jQuery UI Accordion widget.
	/// </summary>
	public class AccordionPanel : ChildPanel {


		#region Constants: Property defaults

		private const bool defaultActive = false;
		private const string defaultHeader = "";

		#endregion


		#region Constructors

		/// <summary>
		/// Initializes a new instance of the <see cref="AccordionPanel"/> class.
		/// </summary>
		public AccordionPanel() : base(HtmlTextWriterTag.Div) { }

		#endregion


		#region Properties: Public

		/// <summary>
		/// Gets or sets whether or not the panel is the currently active panel for the containing control.
		/// </summary>
		[DefaultValue(defaultActive), Category("Appearance")]
		public virtual bool Active {
			get { return ViewState["Active"] as bool? ?? defaultActive; }
			set {
				GroupControlContainer container = Container;
				if(container != null && value) {
					ControlCollection containerControls = container.Controls;
					for(int i = 1; i < containerControls.Count; i+=2) {
						AccordionPanel panel = containerControls[i] as AccordionPanel;
						if(panel == null) continue;
						if(panel != this) panel.SetActive(false);
					}
				}
				SetActive(value);
			}
		}

		/// <summary>
		/// The text that will be rendered into the jQuery UI Accordion widget section's header.
		/// HTML is allowed, so any text must HTML encoded.
		/// </summary>
		[DefaultValue(defaultHeader), PersistenceMode(PersistenceMode.InnerProperty)]
		public virtual string Header {
			get { return ViewState["Header"] as string ?? defaultHeader; }
			set { ViewState["Header"] = value; }
		}

		/// <summary>
		/// Gets or sets whether or not the control performs a postback in the widget's "changestart" option event.
		/// A True value is required in order to receive a <see cref="Activating"/> event.
		/// </summary>
		[DefaultValue(false), Category("Behavior")]
		public virtual bool PostBackOnChangeStart {
			get { return ViewState["PostBackOnChangeStart"] as bool? ?? false; }
			set { ViewState["PostBackOnChangeStart"] = value; }
		}

		#endregion


		#region Events

		/// <summary>
		/// This event is triggered when the panel will become visible as the accordion starts to change.
		/// Attaching one or more handlers to this event will trigger an automatic postback upon user interaction.
		/// The ASP.NET postback will be appended to any <see cref="Accordion.OnClientChangeStart"/> code and mapped to the "changestart" option when the jQuery UI widget is initialized.
		/// </summary>
		[Category("Action")]
		public event EventHandler<AccordionPanelActivatingEventArgs> Activating;

		#endregion


		#region Methods: Event hooks

		/// <summary>
		/// Raises the <see cref="Activating"/> event.
		/// </summary>
		/// <param name="e">The <see cref="AccordionPanelActivatingEventArgs"/> object that contains the event data.</param>
		protected virtual void OnActivating(AccordionPanelActivatingEventArgs e) {
			if(Activating != null) Activating.Invoke(this, e);
		}

		#endregion


		#region Methods: Internal

		internal void OnActivatingInternal(AccordionPanelActivatingEventArgs e) {
			OnActivating(e);
		}

		internal void SetActive(bool value) {
			ViewState["Active"] = value;
		}

		#endregion


	}

}